<?php

class Tourism_order_refund_model extends CI_Model
{
    private $table   = 'tourism_order_refund';
    private $table_2 = 'tourism_order_product';
    private $table_5 = 'tourism_order';
    private $table_6 = 'tourism_order_product_profit';
    private $table_7 = 'user';

    public function total($search_param = array())
    {
        $this->db->from($this->table . ' AS tourism_order_refund');
        $this->db->join($this->table_2 . ' AS tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_refund.order_product_id');
        $this->db->join($this->table_7 . ' AS user', 'user.uid = tourism_order_refund.seller_uid');
        if (!empty($search_param['order_id'])) {
            $this->db->where('tourism_order_product.order_id', $search_param['order_id']);
        }
        if (!empty($search_param['goods_name'])) {
            $this->db->where("(`tourism_order_product`.`goods_name` LIKE '%{$search_param['goods_name']}%') OR (`tourism_order_refund`.`goods_attr_id`='{$search_param['goods_name']}')");
        }
        if (!empty($search_param['seller_name'])) {
            $this->db->where("(`user`.`user_name` LIKE '%{$search_param['seller_name']}%') OR (`user`.`alias_name` LIKE '%{$search_param['seller_name']}%')");
        }
        if (!empty($search_param['user_name'])) {
            $this->db->where("(`tourism_order_refund`.`user_name` LIKE '%{$search_param['user_name']}%') OR (`tourism_order_refund`.`uid`='{$search_param['user_name']}') OR (`tourism_order_refund`.`cellphone`='{$search_param['user_name']}')");
        }
        if (!empty($search_param['status'])) {
            $this->db->where('tourism_order_refund.status', $search_param['status']);
        }
        if (!empty($search_param['flag'])) {
            $this->db->where('tourism_order_refund.flag', $search_param['flag']);
        }
        return $this->db->count_all_results();
    }

    public function page_list($page_num, $num, $search_param = array())
    {
        $this->db->select('tourism_order_refund.*,tourism_order_product.goods_name,user.user_name AS supplier_name,user.alias_name');
        $this->db->from($this->table . ' AS tourism_order_refund');
        $this->db->join($this->table_2 . ' AS tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_refund.order_product_id');
        $this->db->join($this->table_7 . ' AS user', 'user.uid = tourism_order_refund.seller_uid');
        if (!empty($search_param['order_id'])) {
            $this->db->where('tourism_order_product.order_id', $search_param['order_id']);
        }
        if (!empty($search_param['goods_name'])) {
            $this->db->where("(`tourism_order_product`.`goods_name` LIKE '%{$search_param['goods_name']}%') OR (`tourism_order_refund`.`goods_attr_id`='{$search_param['goods_name']}')");
        }
        if (!empty($search_param['seller_name'])) {
            $this->db->where("(`user`.`user_name` LIKE '%{$search_param['seller_name']}%') OR (`user`.`alias_name` LIKE '%{$search_param['seller_name']}%')");
        }
        if (!empty($search_param['user_name'])) {
            $this->db->where("(`tourism_order_refund`.`user_name` LIKE '%{$search_param['user_name']}%') OR (`tourism_order_refund`.`uid`='{$search_param['user_name']}') OR (`tourism_order_refund`.`cellphone`='{$search_param['user_name']}')");
        }
        if (!empty($search_param['status'])) {
            $this->db->where('tourism_order_refund.status', $search_param['status']);
        }
        if (!empty($search_param['flag'])) {
            $this->db->where('tourism_order_refund.flag', $search_param['flag']);
        }
        $this->db->order_by('tourism_order_refund.refund_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    /**
     * 获取退款详情
     * @param unknown $id
     */
    public function getRefund($refund_id)
    {
        $this->db->select('tourism_order_refund.*,tourism_order_product.goods_name,user.user_name,user.alias_name');
        $this->db->from($this->table . ' AS tourism_order_refund');
        $this->db->join($this->table_2 . ' AS tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_refund.order_product_id');
        $this->db->join($this->table_7 . ' AS user', 'user.uid = tourism_order_refund.seller_uid');
        $this->db->where('tourism_order_refund.refund_id', $refund_id);
        return $this->db->get();
    }
    
    public function updateTourismOrderRefund($postData)
    {
        $data = array(
            'counter_fee'    => !empty($postData['counter_fee']) ? $postData['counter_fee'] : 0,
            'refund_content' => $postData['refund_content'],
            'reject_content' => !empty($postData['reject_content']) ? $postData['reject_content'] : '',
            'status'         => $postData['status'],
        );
        $this->db->where('refund_id', $postData['refund_id']);
        return $this->db->update($this->table, $data);
    }
    
    public function updateRefundFlag($postData)
    {
        $data = array('flag' => $postData['flag']);
        $this->db->where('refund_id', $postData['refund_id']);
        return $this->db->update($this->table, $data);
    }
    
    //根据退款ID获取退款信息
    public function findById($refund_id)
    {
        $this->db->where('refund_id', $refund_id);
        return $this->db->get($this->table);
    }

    //更改订单信息
    public function updateOrderInfo($orderProduct, $tourismRefund, $poundage, $num, $order_res)
    {
        $err_code = 0;
        $err_msg = "";

        //计算退货商品的价格 退货数*单价-手续费
        $o_num = $tourismRefund->number;//退货数
        $isTransport = 0;
        //如果$num=0 商品全退
        if (($order_res->order_status == 3) && $num <= 0) {
            $transport_cost = $order_res->transport_cost;
            $isTransport = 1;
        }
        $amount_return = bcsub(bcmul($orderProduct->pay_amount, $o_num, 3),bcmul($orderProduct->swim, $o_num,3),3) ; //相减
        $actual_return = bcsub($amount_return, $poundage, 3); //相减

        if ($actual_return < $poundage) {
            $err_code = 1;
            $err_msg = "别逗了，手续费（.$poundage.）比退款金额（.$actual_return.）还多,还能玩耍不！";
            return array('err_code' => $err_code, 'err_message' => $err_msg);
        }

        $amount_bonus_return = 0;
        if ($num <= 0) {
            $this->db->set('order_status', '1');
        }
        $this->db->set('order_pay_total', "order_pay_total - $amount_return", FALSE);
        if (isset($transport_cost)) {
            $this->db->set('transport_cost', 0);
        }
        $this->db->where('order_id', $orderProduct->order_id);
        $this->db->update($this->table_5);

        $data['amount_bonus_return'] = $amount_bonus_return;
        $data['amount_refund'] = $amount_return;
        $data['err_code'] = $err_code;
        $data['err_message'] = $err_msg;
        $data['actual_return'] = $actual_return;
        $data['isTransport'] = $isTransport;
        $data['transport_cost'] = $order_res->transport_cost;

        return $data;
    }

    /*
     * 获取已退商品总数
     * */
    public function getAllRefundProduct($order_id)
    {
        $this->db->select('sum(p.refund_num) as num');
        $this->db->where('p.order_id', $order_id);
        $result = $this->db->get($this->table_2 . ' as p');
        return $result->num_rows();
    }

    /*
     *获取订单状态
     * */
    public function getTourismStatus($order_id)
    {
        $this->db->select('order_status');
        $this->db->where('order_id', $order_id);
        $result = $this->db->get($this->table_5)->row();
        return isset($result->order_status) ? $result->order_status : 0;
    }

    /**
     * 获取全部已退商品数量
     * @param unknown $order_id
     * @return number
     */
    public function getAllRefundNum($order_id)
    {
        $this->db->select_sum('number');
        $this->db->where('flag', 2); //已经退款了的
        $this->db->where('order_id', $order_id);
        $result = $this->db->get($this->table);
        $row = $result->row();
        return isset($row->number) ? $row->number : 0;
    }
}